package com.example.fence.vo;

import com.example.fence.enums.AlarmMode;
import com.example.fence.util.Result;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;

import static com.example.fence.util.ResultCode.FAIL;
import static com.example.fence.util.ResultCode.SUCCESS;

@Data
@Component
/**
 * {
 *     "devicesName": [
 *         "test2",
 *         "test1"
 *     ],
 *     "alarmMode": [
 *         "email",
 *         "lock"
 *     ],
 *     "sendEmail": [
 *         "service@novaet.com.cn"
 *     ],
 *     "receiveEmail": [
 *         "aaa@qq.com",
 *         "bbb@qq.com"
 *     ],
 *     "sendInterval":10
 * }
 */
//@AllArgsConstructor
public class AlarmVo {
    // 设备imei列表
    private List<String> devicesName;
    // 报警类型
    private List<String> alarmMode;
    // 发送邮箱列表
    private List<String> sendEmail;
    // 接收邮箱列表
    private List<String> receiveEmail;
    // 邮件传输间隔
    @Value("${send.interval}")
    private int sendInterval;


    public AlarmVo(List<String> devicesName, List<String> alarmMode, List<String> sendEmail, List<String> receiveEmail) {
        this.devicesName = devicesName;
        this.alarmMode = alarmMode;
        this.sendEmail = sendEmail;
        this.receiveEmail = receiveEmail;
    }

    // 校验上传的报警类型是否在报警列表中
    public Result validateAlarmModes() {
        for (String mode : alarmMode) {  // 遍历上传的报警类型判断是否都在报警列表中
            boolean found = false;  // 默认不在枚举列表中
            for (AlarmMode alarm : AlarmMode.values()) {  // 遍历枚举列表依次判断是否符合
                if (mode.equalsIgnoreCase(alarm.name())) {  // 在枚举列表中
                    found = true;
                    break;  // 进行下一个传入参数的判断
                }
            }
            if (!found) {  // 当前mode不在报警枚举列表中，上传了错误的报警类型
                return new Result(FAIL.code, false, alarmMode,mode+" is not in AlarmMode List!"); // 发现不匹配的报警类型
            }
        }
        return new Result(SUCCESS.code,true,alarmMode,"success"); // 所有报警类型均匹配
    }

}
